package aes.server;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

/**
 * This is the frame that starts the server
 */
public class ServerGui extends JFrame {


	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JLabel lblNewLabel_2;
	private JLabel lblServerLogin;
	private JTextField textPort;
	private JTextField textDbLogin;
	private JTextField textDbPass;
	private JButton btnStartSrv;
	private JButton btnClose;
	private JButton btnStop;
	private JSeparator separator;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ServerGui frame = new ServerGui();
					frame.setTitle("Server");
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ServerGui() {
		setTitle("Server Login");
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 401, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		textPort = new JTextField("3606");
		textPort.setBounds(210, 42, 86, 20);
		textPort.setColumns(10);
		
		textDbLogin = new JTextField("root");
		textDbLogin.setBounds(210, 80, 86, 20);
		textDbLogin.setColumns(10);
		
		textDbPass = new JTextField();
		textDbPass.setBounds(210, 118, 86, 20);
		textDbPass.setColumns(10);
		
		btnStartSrv = new JButton("Start");
		btnStartSrv.setBounds(92, 165, 69, 23);
		btnStartSrv.addActionListener(new ActionListener() {
			
			/* (non-Javadoc)
			 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
			 */
			public void actionPerformed(ActionEvent e) {
				
				ServerLogic.dblogin=textDbLogin.getText();
				ServerLogic.dbpass=textDbPass.getText();
				AesServer.startNewServer(textPort.getText());
			}
		});
		
		btnClose = new JButton("Close");
		btnClose.setBounds(237, 165, 69, 23);
		btnClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		
		lblNewLabel = new JLabel("Server Port");
		lblNewLabel.setBounds(92, 45, 69, 14);
		
		lblNewLabel_1 = new JLabel("DB Login");
		lblNewLabel_1.setBounds(92, 83, 55, 14);
		
		lblNewLabel_2 = new JLabel("DB Password");
		lblNewLabel_2.setBounds(92, 121, 86, 14);
		
		btnStop = new JButton("Stop");
		btnStop.setBounds(164, 165, 69, 23);
		btnStop.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					ServerLogic.server.close();
					JOptionPane.showMessageDialog(null,  "Server Closed","",JOptionPane.INFORMATION_MESSAGE);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}	
			}
		});
		contentPane.setLayout(null);
		contentPane.add(lblNewLabel);
		contentPane.add(lblNewLabel_1);
		contentPane.add(lblNewLabel_2);
		contentPane.add(btnStartSrv);
		contentPane.add(textDbPass);
		contentPane.add(textDbLogin);
		contentPane.add(textPort);
		contentPane.add(btnStop);
		contentPane.add(btnClose);
		
		lblServerLogin = new JLabel("Server Login");
		lblServerLogin.setFont(new Font("Tempus Sans ITC", Font.BOLD, 21));
		lblServerLogin.setBounds(133, 2, 129, 29);
		contentPane.add(lblServerLogin);
		
		separator = new JSeparator();
		separator.setBounds(5, 32, 385, 2);
		contentPane.add(separator);
	}
}
